use alice2d::ui::egui::{Ui, vec2};

use crate::editor::EditorLayer;

use super::inspect_panel::load_texture_to_egui;

const ASSETS_PATH: &'static str = "assets";

const FILE_ICON: &'static str = "assets/icon/FileIcon.png";
const FOLDER_ICON: &'static str = "assets/icon/DirectoryIcon.png";


pub struct File {
    pub name: String,
    pub path: String,
}

pub struct Folder {
    pub name: String,
    pub path: String,
}
pub struct AssetPanel;

impl AssetPanel {
    pub fn draw_assets(layer:&mut EditorLayer, ui:&mut Ui){

        let path = std::path::Path::new(&layer.asset_path);

        let mut files = Vec::new();
        let mut folders = Vec::new();
        if &layer.asset_path != ASSETS_PATH {
            folders.push(Folder{
                name: "..".to_string(),
                path:std::path::Path::new(&layer.asset_path).parent().unwrap().to_str().unwrap().to_string(),
            });
        }
        for entry in path.read_dir().unwrap() {
            let entry = entry.unwrap();
            let path = entry.path();
            if path.is_file() {
                let name = path.file_name().unwrap().to_str().unwrap();
                files.push(File{
                    name: name.to_string(),
                    path: path.to_str().unwrap().to_string(),
                });
            }else if path.is_dir() {
                let name = path.file_name().unwrap().to_str().unwrap();
                folders.push(Folder {
                    name: name.to_string(),
                    path: path.to_str().unwrap().to_string(),
                });
            }
        }

            ui.horizontal_wrapped(|ui|{

                for folder in &folders {

                    ui.allocate_ui_with_layout(vec2(80.0_f32, 80.0_f32),alice2d::ui::egui::Layout::top_down(alice2d::ui::egui::Align::TOP), |ui|{

                        ui.vertical_centered(|ui|{

                            if !layer.custom_image_texture.contains_key(FOLDER_ICON) {
                              
                                if let Some(handle) = load_texture_to_egui(ui,FOLDER_ICON) {
                                    layer.custom_image_texture
                                    .insert(FOLDER_ICON.to_owned(), handle);
                                }

                            }
                            if let Some(handle) = layer.custom_image_texture.get(&FOLDER_ICON.to_owned()) {
                                // Show the image:
                                
                           

                                if ui.add( alice2d::ui::egui::ImageButton::new(handle, vec2(50.0_f32, 50.0_f32))).clicked() {
                                    layer.asset_path = folder.path.clone();
                                }

                            }

                            ui.label(&folder.name);

                        });
                    });

                }

                for file in &files {
                    ui.allocate_ui(vec2(80.0_f32, 80.0_f32), |ui|{

                       ui.vertical_centered(|ui|{

                            if !layer.custom_image_texture.contains_key(FILE_ICON) {

                                if let Some(handle) = load_texture_to_egui(ui,FILE_ICON) {
                                    layer.custom_image_texture
                                    .insert(FILE_ICON.to_owned(), handle);
                                }

                            }
                            if let Some(handle) = layer.custom_image_texture.get(&FILE_ICON.to_owned()) {
                                // Show the image:
                                ui.image(handle, vec2(50.0_f32, 50.0_f32));
                            }
                     
                            ui.label(&file.name);
                        })
                    });

                }

            });

    }
}